package com;

import org.appformer.maven.integration.ArtifactResolver;
import org.appformer.maven.integration.DependencyDescriptor;
import org.appformer.maven.integration.embedder.MavenSettings;
import org.appformer.maven.support.AFReleaseId;
import org.appformer.maven.support.AFReleaseIdImpl;
import org.appformer.maven.support.DependencyFilter;
import org.eclipse.aether.artifact.Artifact;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

@Component
public class ModelMavenLoader{
    public ModelMavenLoader(){
//        String path = ModelMavenLoader.class.getClassLoader().getResource("settings.xml").getPath();
        System.setProperty(MavenSettings.CUSTOM_SETTINGS_PROPERTY,"D:\\gfworker\\mavendemo\\mavendemo\\settings.xml");
        try {
            loadWithDependency();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void loadWithDependency() throws Exception{
        //下载依赖jar包
        ArtifactResolver artifactResolver = new ArtifactResolver();
        AFReleaseId afReleaseId = new AFReleaseIdImpl("junit:junit:4.11");
        Artifact artifact = artifactResolver.resolveArtifact(afReleaseId);

        List<DependencyDescriptor> dependecies = artifactResolver.getArtifactDependecies(artifact.getArtifactId());

        File artifactJar = artifact.getFile();

        List<File> dependencyFiles = new LinkedList<>();
        for (DependencyDescriptor depend : dependecies) {
            if (!DependencyFilter.COMPILE_FILTER.accept(depend.getReleaseId(), depend.getScope())) {
                continue;
            }
            Artifact a = artifactResolver.resolveArtifact(depend.getReleaseId());
            dependencyFiles.add(a.getFile());
        }

    }
}